//package org.base.common.shiro;
//
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.collections.CollectionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.shiro.authc.*;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.base.ConstantPool;
//import org.base.sys.VO.SysUserVO;
//import org.base.sys.pojo.SysLoginLog;
//import org.base.sys.pojo.SysRole;
//import org.base.sys.service.SysLoginLogService;
//import org.base.sys.service.SysRoleService;
//import org.base.sys.service.SysUserService;
//import org.base.common.exception.BusinessException;
//import org.base.common.exception.ExceptionEnum;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import java.util.List;
//import java.util.Optional;
//
///**
// * @author He mingliang
// * @date 2020/5/31
// */
//
//@Slf4j
//public class CustomRealm extends AuthorizingRealm {
//
//    @Autowired
//    private SysUserService userService;
//
//    @Autowired
//    private SysRoleService roleService;
//
//
//    /**
//     * 获取用户角色，权限信息
//     * @param principalCollection
//     * @return
//     */
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        String username = (String) principalCollection.getPrimaryPrincipal();
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//        final SysUserVO user = userService.getUserVOByLoginName(username);
//        if (user != null) {
//            final List<SysRole> roles = user.getRoles();
//            if (CollectionUtils.isNotEmpty(roles)) {
//                final Optional<SysRole> adminRole = roles.stream().filter(e -> e.getCode() != null && e.getCode().equalsIgnoreCase(ConstantPool.ADMIN_CODE)).findFirst();
//                if (adminRole.isPresent()) {
//                    info.addStringPermission(ConstantPool.ADMIN);
//                } else {
//                    List<String> rolePerms = userService.getRolesPermissions(roles);
//                    info.addStringPermissions(rolePerms);
//                }
//            }
//        }
//        return info;
//    }
//
//    /**
//     * 认证
//     * @param authenticationToken
//     * @return
//     * @throws AuthenticationException
//     */
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
//        String username =  token.getUsername();
//        char[] passwordObject = token.getPassword();
//
//        if (passwordObject == null) {
//            BusinessException.throwEx(ExceptionEnum.UNKNOWN_ACCOUNT);
//        }
//        String password = new String(passwordObject);
//        if (StringUtils.isBlank(password)) {
//            BusinessException.throwEx(ExceptionEnum.UNKNOWN_ACCOUNT);
//        }
//        SysUserVO userVO = userService.login(username, password);
//        return new SimpleAuthenticationInfo(userVO, password, CustomRealm.class.getName());
//    }
//
//}
